package xkp.lesson2;

import java.io.*;
import java.net.Socket;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",8888);
        File file = new File("C:\\Users\\Administrator\\Desktop\\hosts");
        //不但可以可以传输字节，也可以按照编码传输字符
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
        byte[] buf = new byte[1024];

        //1、传输文件名称
        dataOutputStream.writeUTF(file.getName());
        //2、循环从文件中读取数据，然后传输
        int length =bufferedInputStream.read(buf);
        while(length != -1){
            dataOutputStream.write(buf,0,length);
            length =bufferedInputStream.read(buf);
        }

        dataOutputStream.flush();
        dataOutputStream.close();
        bufferedInputStream.close();
        socket.close();
    }
}
